#include <iostream>
#include <map>
using namespace std;

void caozuo();

int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
        caozuo();
}

void caozuo()
{
    map<string,int>l;
    int n;
    cin>>n;
    string s;
    for (int i=0;i<n;i++)
    {
        cin>>s;
        if (l.find(s)==l.end())
            l[s]=1;
        else
            l[s]+=1;
    }
    for (auto i:l)
    {
        cout<<i.first<<" "<<i.second<<endl;
    }
}